#!/bin/bash
# Filename:21Deploy
# Author:testdream
# Date:20160112

#脚本使用说明
#1、通过指定命令行的第1个参数以确定部署哪个服务，如部署全部则参数为all.
#all 全部更新
#translator 更新248的媒体转换服务
#restapi
#appstore
#session
#organization
#clientcenter
#schd
#manager  管理后台
#serveplatform
#terminal
#poggyio-apn
#mbox
#safeguard
#social
#poggyio-uc
#domainadmin
#meeting
#extras-ldap

#host变量定义
remoteAddr=xxx.xxx.xxx.25
translatorHost=xxx.xxx.xxx.248

#路径定义
deployfiles=/opt/test
remotePath=/var/poggyio
remoteManPath=/var/poggyio/apache-tomcat-8.0.24/webapps/poggyio
targetPath=/opt/workplus
spript_xpath_248=/home/atwork/autoDeploy

#复制25服务到本地
if [ -z $2 ];then		
	echo "================================Delete files now!=============================="
	rm -rf $deployfiles/test/*
	echo "================================Copy File Now!================================"
	scp -r -P 22 root@$remoteAddr:$remotePath/poggyio-*  $deployfiles/test

	#复制后台服务
	mkdir $deployfiles/test/manager
	scp -r -P 22 root@$remoteAddr:$remoteManPath/*  $deployfiles/test/manager/

	for sinFile in $deployfiles/test/*;do
		if [ -f $sinFile ];then
			rm -f $sinFile
		fi
	done
fi

#执行函数-参数1为部署目标
function deploy21(){
	echo "=================================Deploy now!===================================="
	i=0
	#覆盖配置文件
	#覆盖conf文件夹
	echo "==============================Update config file!==============================="
	cd $deployfiles/test
	for sinFile in `find ./ -type d  -name  "*$1*"`;do
	echo $sinFile
	echo $deployfiles/conf/$1/
		if [ -d $sinFile ];then
			cp -rf $deployfiles/conf/$1/* $sinFile/conf		
			i=$[ $i+1 ]
		else
			rm -f $sinFile
		fi
	done


	#文件夹是否多于1个
	if [ $i -gt 1 ];then
		echo "=============Oops! The $1 folder more then one!Stop!===================="
		exit 1
	fi
		
	
	#备份删除服务
	echo "==========================Stop service and backup files!=========================="
	pid=`jps -v |grep $1|awk '{print $1}'`
	if [ -n "$pid" ];then
		kill -9 $pid
		echo "============The PID of $1 is deleted successfully!====================="
	fi
		
	#删除软链
	cd 	$targetPath
	rm $1
	#备份并更新名称
	i=0
	for sinFold in `find ./ -type d  -name  "*-$1*"`;do
	echo $sinFold
		if [ -d $sinFold ];then
			mv $targetPath/*-$1*  $targetPath/bakFiles/$(basename $sinFold)-$(date +%y%m%d%H%M%S)		
			i=$[ $i+1 ]
		else
			rm -f $sinFold
		fi
	done

	if [ $i -gt 1 ];then
		echo "=============Oops! The $1 folder more then one!Stop!===================="
	fi
	
	rm -rf $targetPath/*$1*
		
	#复制目标文件夹
	cp -r $deployfiles/test/*$1*  $targetPath/
		
	#启动应用
	echo "==========================Add soft link and start service!========================="
	ln -s $targetPath/*$1*  $targetPath/$1
	
	if [ -n "$pid" ];then
		j=0
		while(($j<60))
		do
			#echo $pid
			port=`netstat -ntpl|grep $pid |awk '{print $1}'`
			if [ -z "$port" ];then
				break
			fi
			sleep 2
			j=$[ $j+1 ]
		done
	fi
	
	sh  $targetPath/$1/bin/start.sh
}


#248服务器部署
echo "=================================Deploy now!===================================="
case $1 in 
	translator|all)
		ssh -p3701 atwork@$translatorHost "sh $spript_xpath_248/248Deploy_1.sh translator"

	;;&
	safeguard|all)
		deploy21 safeguard
	;;&
	extras-ldap|all)
		deploy21 extras-ldap
	;;&
	
	appstore|all)
		deploy21 appstore
	;;&
	restapi|all)
		deploy21 restapi
	;;&
	poggyio-uc|all)
		deploy21 poggyio
	;;&
	session|all)
        deploy21 session
	;;&
	organization|all)
		deploy21 organization
	;;&
	clientcenter|all)
		deploy21 clientcenter
	;;&
	social|all)
		deploy21 social
	;;&
	
	schd|all)
		deploy21 schd
	;;&
	mbox|all)
		deploy21 mbox
	;;&
	manager|all)
		ssh -p22 $translatorHost "sh $deployfiles/248Recover_1.sh manager"
	;;&
	poggyio-apn|all)
		deploy21 poggyio-apn
	;;&
	domainadmin|all)
		deploy21 domainadmin
	;;&
	serveplatform|all)
		deploy21 serveplatform
	;;&
	terminal|all)
		deploy21 terminal
	;;&
	meeting|all)
		deploy21 meeting
	;;&
	pan|all)
		deploy21 pan
	;;&

esac

exit 0 
